//
//  LHYPDFEditMoreToolVC.m
//  DrawLine
//
//  Created by lhy on 2021/3/3.
//

#import "LHYPDFEditMoreToolVC.h"
#import "LHYPDFEditSignAddListView.h"

// 点击cell 通知
static NSString *const PDFMoreToolDidSelectRowNotification = @"PDFMoreToolDidSelectRowNotification";

@interface LHYPDFEditMoreToolVC () <UITableViewDelegate, UITableViewDataSource, UIPopoverPresentationControllerDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) UIButton *footerViewBtn;

@end

@implementation LHYPDFEditMoreToolVC

- (instancetype)initWithPreferredContentSize:(CGSize)size
                                     dataArr:(NSArray *)dataArr
                      modalPresentationStyle:(UIModalPresentationStyle)style
                                  sourceView:(UIView *)sourceView
                                  sourceRect:(CGRect)sourceRect
                    permittedArrowDirections:(UIPopoverArrowDirection)permittedArrowDirections {
    self = [super init];
    if (self) {
        self.popoverType = TypeImageTitle;
        self.dataArr = dataArr;
        self.preferredContentSize = size;
        self.modalPresentationStyle = style;
        self.popoverPresentationController.sourceView = sourceView;
        self.popoverPresentationController.sourceRect = sourceRect; //指定箭头所指区域的矩形框范围（位置和尺寸），以view的左上角为坐标原点
        self.popoverPresentationController.permittedArrowDirections = permittedArrowDirections; //箭头方向
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self updateUIWithType:TypeImageTitle];
}

- (void)updateUIWithType:(NSInteger)type {
    if (type == TypeTitleBtn) {
        self.tableView.tableFooterView = self.footerViewBtn;
    } else {
        self.tableView.tableFooterView = [[UIView alloc] init];
    }
}

/// 添加或移除签名 action
- (void)actionFooterViewBtn {
    //    1、判断是否已有签名，若有打开 LHYPDFEditSignAddListView；
    //                     否则打开 LHYSignAlertView
    
    NSDictionary *par = @{@"type":@(TypeShowSignViewBtn)};
    //    NSDictionary *par = @{@"type":@(TypeShowSignListViewBtn)};
    [[NSNotificationCenter defaultCenter] postNotificationName:PDFMoreToolDidSelectRowNotification object:nil userInfo:par];
    [self dismissViewControllerAnimated:YES completion:nil];
}



#pragma mark - 代理

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifer = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%@", self.dataArr[indexPath.row]];
    if (self.popoverType == TypeImageTitle) {
        cell.textLabel.textAlignment = NSTextAlignmentLeft;
        if (indexPath.row == 0) {
            cell.imageView.image = [UIImage imageNamed:@"lmr_text"];
        } else if (indexPath.row == 1) {
            cell.imageView.image = [UIImage imageNamed:@"lmr_sign"];
        }
    } else {
        cell.textLabel.textAlignment = YES;
        cell.imageView.image = nil;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //    图片+文字
    if (self.popoverType == TypeImageTitle) {
        
        switch (indexPath.row) {
            case 0:{// 文本
                NSDictionary *par = @{@"index":@(indexPath.row),
                                      @"text":@"文本",
                                      @"type":@(TypeImageTitle)};
                [[NSNotificationCenter defaultCenter] postNotificationName:PDFMoreToolDidSelectRowNotification object:nil userInfo:par];
                [self dismissViewControllerAnimated:YES completion:nil];
            }
                break;
            case 1:{// 签名
                //            更新布局
                self.preferredContentSize = CGSizeMake(150, 150 + 40);
                [self loadViewIfNeeded];
                //            更新数据
                self.popoverType = TypeTitleBtn;
                [self updateUIWithType:TypeTitleBtn];
                self.dataArr = [NSMutableArray arrayWithArray:@[@"完工", @"未完工", @"检验"]];
                [self.tableView reloadData];
            }
                break;
                
            default:
                break;
        }
    }
    //    文字（完工/未完工/检验）
    else if (self.popoverType == TypeTitleBtn) {
        
        NSDictionary *par = @{@"index":@(indexPath.row),
                              @"text":self.dataArr[indexPath.row],
                              @"type":@(TypeTitleBtn)};
        [[NSNotificationCenter defaultCenter] postNotificationName:PDFMoreToolDidSelectRowNotification object:nil userInfo:par];
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

#pragma mark - 懒加载

- (NSArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSArray alloc] init];
    }
    return _dataArr;
}

- (UIButton *)footerViewBtn {
    if (!_footerViewBtn) {
        _footerViewBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
        _footerViewBtn.backgroundColor = UIColor.whiteColor;
        [_footerViewBtn setTitleColor:UIColor.systemBlueColor forState:UIControlStateNormal];
        _footerViewBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_footerViewBtn setTitle:@"添加或移除签名" forState:UIControlStateNormal];
        [_footerViewBtn addTarget:self action:@selector(actionFooterViewBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _footerViewBtn;
}

@end
